package com.deryun.tyl.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.aspectj.bridge.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.ContentMessage;
import com.deryun.tyl.bean.User;
import com.deryun.tyl.bean.tempBean.FriendConnect;
import com.deryun.tyl.bean.tempBean.MyPublish;
import com.deryun.tyl.service.impl.ContentMessageServiceImpl;

@Controller
@RequestMapping("contentMessage")
public class ContentMessageController 
{
	
	@Resource(name="contentMessageServiceImpl")
	private ContentMessageServiceImpl contentMessageSI;
	
	//添加内容
	@RequestMapping("/addMassage")
	public String addMassage(ContentMessage message, @RequestParam("oneKeyPic1") MultipartFile oneKeyPic1, HttpServletRequest request) throws Exception
	{
		User user = (User)request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		if (message.plate.equals("一键通介绍")) 
		{
			contentMessageSI.delCMInfo();
		}
		String path = "/virtualhost/jbwz/ROOT/upload/image";   
		if (!oneKeyPic1.isEmpty()) {
			String oneKeyPicFileName = String.valueOf(System.currentTimeMillis()) + ".png";
			File oneKeyPicFile = new File(path, oneKeyPicFileName);  
			if(!oneKeyPicFile.exists())
			{  
				oneKeyPicFile.mkdirs();  
			}  
			oneKeyPic1.transferTo(oneKeyPicFile);  
			message.setOneKeyPic("upload/image/"+oneKeyPicFileName);
		}
		if (request.getParameter("type").equals("3")||request.getParameter("type").equals("2")) {
			if (request.getParameter("region0").equals("1")) { 
				String region = "1";
				for (int i = 1; i <= 4 ; i++) {
					if (!request.getParameter("region"+i).equals("0")) {
						region = region + "|" + request.getParameter("region"+i);
					}
				}
				message.setRegion(region);
			}
		}
		message.setAddUserId(user.getId().toString());
		message.setAddUserName(user.getContactMan());
		message.setSupplier(user.getInstitutionName());
		message.setIsPass("0");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		message.setCreateTime(df.format(new Date()));
		contentMessageSI.addCM(message);
		List<ContentMessage> myPublish = contentMessageSI.getAll(user.getId().toString());
		request.setAttribute("myPublish", myPublish);
		return "redirect:/ftoac/loadTrace";
	}
	
	//修改内容
	@RequestMapping("/modifyCM")
	public String modifyCM(ContentMessage message,@RequestParam("oneKeyPic1") MultipartFile oneKeyPic1,HttpServletRequest request) throws IllegalStateException, IOException{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (request.getParameter("type").equals("3")||request.getParameter("type").equals("2")) {
			if (request.getParameter("region0").equals("1")) {
				String region = "1";
				for (int i = 1; i <= 4 ; i++) {
					if (!request.getParameter("region"+i).equals("0")) {
						region = region + "|" + request.getParameter("region"+i);
					}
				}
				message.setRegion(region);
			}
		}
		String path = "/virtualhost/jbwz/ROOT/upload/image";   
		if (!oneKeyPic1.isEmpty()) {
			String oneKeyPicFileName = String.valueOf(System.currentTimeMillis()) + ".png";
			File oneKeyPicFile = new File(path, oneKeyPicFileName);  
			if(!oneKeyPicFile.exists())
			{  
				oneKeyPicFile.mkdirs();  
			}  
			oneKeyPic1.transferTo(oneKeyPicFile);  
			message.setOneKeyPic("/upload/image"+oneKeyPicFileName);
		}
		contentMessageSI.modifyCM(message);
		List<ContentMessage> myPublish = contentMessageSI.getAll(user.getId().toString());
		request.setAttribute("myPublish", myPublish);
		return "redirect:/ftoac/loadTrace";
	}
	
	//获取详细内容
	@RequestMapping("/getCM")
	public String getCM(@RequestParam("id") int id,HttpServletRequest request){
		ContentMessage message = contentMessageSI.getCM(id);
		request.setAttribute("message", message);
		return "back_admin_manger/detal_content";
	}
	
	@RequestMapping("/getCount")
	public String getCount(HttpServletRequest request){
		User user = (User)request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		MyPublish message = contentMessageSI.getCount(user.getId().toString());
		request.setAttribute("message", message);
		return "back_admin_manger/myPublish";
	}
	
	@RequestMapping("/getMyContent")
	public String getMyContent(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		
		if (request.getSession().getAttribute("myPublish") == null) {
			List<ContentMessage> messages = contentMessageSI.getAll(user.getId().toString());
			request.getSession().setAttribute("myPublish", messages);
		}
		
		return "back_admin_manger/myContent";
	}
	
	@RequestMapping("/getMyProduct")
	public String getMyProduct(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (request.getSession().getAttribute("myPublish") == null) {
			List<ContentMessage> messages = contentMessageSI.getAll(user.getId().toString());
			request.getSession().setAttribute("myPublish", messages);
		}
		return "back_admin_manger/myProduct";
	}
	
	@RequestMapping("/getMyProject")
	public String getMyProject(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (request.getSession().getAttribute("myPublish") == null) {
			List<ContentMessage> messages = contentMessageSI.getAll(user.getId().toString());
			request.getSession().setAttribute("myPublish", messages);
		}
		return "back_admin_manger/myProject";
	}
	@RequestMapping("/getMyActivity")
	public String getMyActivity(HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		if (request.getSession().getAttribute("myPublish") == null) {
			List<ContentMessage> messages = contentMessageSI.getAll(user.getId().toString());
			request.getSession().setAttribute("myPublish", messages);
		}
		return "back_admin_manger/myActivity";
	}
	
	@RequestMapping("/deleteCM")
	public String deleteCM(int id,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		contentMessageSI.deleteCM(id);
		List<ContentMessage> myPublish = contentMessageSI.getAll(user.getId().toString());
		request.setAttribute("myPublish", myPublish);
		return "redirect:/ftoac/loadTrace";
	}
	
	//通过审批
	@RequestMapping("/accessCM")
	public String accessCM(int id,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		contentMessageSI.accessCM(id);
		List<ContentMessage> message = contentMessageSI.getAdminAuthCM();
		request.getSession().setAttribute("authCM", message);
		return "back_admin_manger/content_message_auth";
	}
	
	//拒绝审批
	@RequestMapping("/refuseCM")
	public String refuseCM(int id,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		contentMessageSI.refuseCM(id);
		List<ContentMessage> message = contentMessageSI.getAdminAuthCM();
		request.getSession().setAttribute("authCM", message);
		return "back_admin_manger/content_message_auth";
	}
	
	//admin删除内容
	@RequestMapping("/adminDelCM")
	public String adminDelCM(int id,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		contentMessageSI.deleteCM(id);
		List<ContentMessage> message = contentMessageSI.getAdminAuthCM();
		request.getSession().setAttribute("authCM", message);
		return "back_admin_manger/content_message_auth";
	}
	
	//admin查询内容
	@RequestMapping("/searchCM")
	public String searchCM(@RequestParam("plate") String plate,@RequestParam("title") String title,@RequestParam("type") String type, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			return "redirect:/civil/homepage";
		}
		String ttile = "%"+title+"%";
		List<ContentMessage> message = contentMessageSI.searchCM(plate, ttile);
		request.setAttribute("message", message);
		if (type.equals("1")) {
			return "back_admin_manger/content_base";
		}
		if (type.equals("2")) {
			return "back_admin_manger/product_base";
		}
		if (type.equals("3")) {
			return "back_admin_manger/project_base";
		}else{
			return "redirect:/civil/homepage";
		}
	}
	
	//前台查询列表
	@RequestMapping("/searchVCM")
	public String searchVCM(
			@RequestParam("startTime") String startTime,
			@RequestParam("endTime") String endTime,
			@RequestParam("plate") String plate,
			@RequestParam("title") String title,
			@RequestParam("grade") String grade, 
			HttpServletRequest request) throws UnsupportedEncodingException{
		System.out.println(startTime);
		System.out.println(endTime);
		System.out.println(plate);
		System.out.println(title);
		System.out.println(grade);
		String ttile = "";
		if (!title.equals("")) {
			ttile = "%"+title+"%";
		}
		
		if (grade.equals("不限")) {
			grade = "";
		}
		
		List<ContentMessage> message = contentMessageSI.searchVCM(startTime,endTime,plate,grade,ttile);
		request.setAttribute("message", message);
		if (plate.equals("一键通快讯")) {
			return "civil_service/onekey_list_page";
		}
		if (plate.equals("政策法规")) {
			return "civil_service/policies_regulations";
		}
		if (plate.equals("标准规范")) {
			return "civil_service/standard_specification";
		}
		if (plate.equals("专题新闻")) {
			return "civil_service/special_news";
		}
		if (plate.equals("采购公告")) {
			return "civil_service/purchase_announcement";
		}
		if (plate.equals("采购结果公告")) {
			return "civil_service/purchase_announcement_result";
		}
		if (plate.equals("一键通产品")) {
			return "civil_service/product_list";
		}
		if (plate.equals("一键通活动")) {
			return "civil_service/activity_list";
		}
		if (plate.equals("融资动态")) {
			return "civil_service/financing_list";
		}
		if (plate.equals("融资产品")) {
			return "civil_service/financing_product_list";
		}
		else{
			return "civil_service/onekey_list_page";
		}
	}
	//前台查询列表
	@RequestMapping("/searchZFCG")
	public String searchZFCG(HttpServletRequest request) throws UnsupportedEncodingException{
		String projectCategory = request.getParameter("projectCategory");
		String plate = request.getParameter("plate");
		String procurementMode = request.getParameter("procurementMode");
		String heading = request.getParameter("heading");
		String region0 = request.getParameter("region0");
		String region1 = request.getParameter("region1");
		String region2 = request.getParameter("region2");
		String region3 = request.getParameter("region3");
		String region4 = request.getParameter("region4");
		String title = request.getParameter("title");
		String id = request.getParameter("id");
		String supplier = request.getParameter("supplier");
		String addUserName = request.getParameter("addUserName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		
		ContentMessage message = new ContentMessage();
		message.setProjectCategory(projectCategory);
		message.setPlate(plate);
		message.setProcurementMode(procurementMode);
		message.setHeading(heading);
		
		String ttile = "";
		if (!title.equals("")) {
			ttile = "%"+title+"%";
		}
		message.setTitle(ttile);
		if (id != "") {
			message.setId(Integer.parseInt(id));
		}
		
		String tsupplier = "";
		if (!supplier.equals("")) {
			tsupplier = "%"+supplier+"%";
		}
		message.setSupplier(tsupplier);
		
		String taddUserName="";
		if (!addUserName.equals("")) {
			tsupplier = "%"+taddUserName+"%";
		}
		message.setAddUserName(taddUserName);
		message.setStartTime(startTime);
		message.setEndTime(endTime);
		List<ContentMessage> messages = new ArrayList<ContentMessage>();
		String region = "";
		if (request.getParameter("region0").equals("1")) { 
			region = "1";
			for (int i = 1; i <= 4 ; i++) {
				if (!request.getParameter("region"+i).equals("0")) {
					region = request.getParameter("region"+i);
					message.setRegion(region);
					messages.addAll(contentMessageSI.searchZFCG(message));
				}
				return "civil_service/purchase_announcement";
			}
		}
		message.setRegion(region);
		messages = contentMessageSI.searchZFCG(message);
		request.setAttribute("message", messages);
		return "civil_service/purchase_announcement";

	}
	
	//设置友情链接
	@RequestMapping("/friendConnect")
	public String friendConnect(HttpServletRequest request)
	{
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) 
		{
			return "redirect:/civil/homepage";
		}
		try 
		{
			List<FriendConnect> friendConnects = contentMessageSI.getFC();
			for(FriendConnect f : friendConnects)
			{
				String nameNum = "name"+f.getId(); 
				String positionNum = "position"+f.getId(); 
				if(null != request.getParameter(nameNum) && null != request.getParameter(positionNum))
				{	
					if(!f.getTitle().trim().equals(request.getParameter(nameNum).trim()) || !f.getAddress().trim().equals(request.getParameter(positionNum).trim()))
					{
						contentMessageSI.modifyFC(request.getParameter(nameNum).trim(), request.getParameter(positionNum).trim(),String.valueOf(f.getId()));
					}
				}	
			}
			request.setAttribute("result", "success");
			return "forward:/ftoac/toFriendConnect";
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
			request.setAttribute("result", "failed");
			return "forward:/ftoac/toFriendConnect";
		}
	}
}
